<?php

namespace app\admin\controller\user;

use app\admin\model\Sysmessage;
use app\admin\model\yalimedia\Course;
use app\common\controller\Backend;
use app\common\library\Auth;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class UserHistory extends Backend
{

    protected $relationSearch = true;


    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('user_history');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            try {
                $list = $this->model->field('id,type,user_id,nums,course_id,taobaoorder,from_unixtime(createtime)createtime,remark,updatetime')
                    ->where($where)
                    ->where('user_id',input('pid'))
                    ->where('type',1)
                    ->order($sort, $order)
                    ->paginate($limit);
            }catch (\Exception $e){
                halt($e);
            }

//            foreach ($list as $k => $v) {
//                $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
//                $v->hidden(['password', 'salt']);
//            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $this->view->assign('ids',input('ids'));
        return $this->view->fetch();
    }

    public function set_user_course()
    {

        $ids = input()['ids'];
        $pid = input()['pid'];
        $has = $this->model->whereIn('course_id',$ids)->where('user_id',$pid)->select();
        if($has){
            $this->model->whereIn('course_id',$ids)->where('user_id',$pid)->delete();
        }

        $this->model->transaction(function()use($ids,$pid){
            $insertdata = [];
            $message = [];
            foreach ($ids as $v){
                $insertdata[] = ['user_id'=>$pid, 'course_id'=>$v, 'createtime'=>time(), 'updatetime'=>time()];
                $title = Course::where('id',$v)->value('title');
                $message [] = [
                    'createtime'=>time(),
                    'user_id'=>$pid,
                    'title'=>'课程购买通知',
                    'sub_title'=>'您购买的《'.$title.'》课程已下发。',
                    'content'=>'您购买的《'.$title.'》课程已于'.date('Y-m-d H:i:s').'下发。请前往课程页查看'
                ];
            }
            Sysmessage::insertAll($message);
            $this->model->insertAll($insertdata);
        });

        $this->success("添加成功");
    }

    /**
     * 查看
     */
    public function index1()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            try {
                $list = $this->model->with('course')
                    ->field('user_history.id,taobaoorder,from_unixtime(user_history.createtime)createtime,remark,course.cover,course.title coursename')
                    ->where($where)
                    ->where('user_history.type',0)
                    ->order($sort, $order)
                    ->paginate($limit);

            }catch (\Exception $e){
                halt($e);
            }

//            foreach ($list as $k => $v) {
//                $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
//                $v->hidden(['password', 'salt']);
//            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $this->view->assign('ids',input('ids'));
        return $this->view->fetch();
    }
}
